package org.fengdi.order.controller;

import lombok.extern.slf4j.Slf4j;
import org.fengdi.order.entity.Order;
import org.fengdi.order.entity.orderReq.CreateOrderReq;
import org.fengdi.order.service.OrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/order")
public class OrderController {
    @Autowired
    private OrderServiceImpl orderService;

    // 创建订单
    @PostMapping
    public String create(@ModelAttribute CreateOrderReq req) {
        Order order = new Order(
            req.getUser_id(),
            req.getCommodity_code(),
            req.getCount(),
            req.getMoney()
        );

        try {
            this.orderService.createOrder(order);
        } catch (Exception e) {
            log.error("create order error", e);
            return "create order error";
        }
        return "create order success";
    }
}
